﻿@model WebUI.Models.QizSmsSubjectMonthIdx
@{
    var QIDS = Model.QizSmsSubject.QizSubject.QizSubjectDefines.Select(v => v.QizItemID).ToList();
    var subQiz = Model.QizSmsSubject.QizSubject.SubQizSubjects;
    List<string> subQIDS = new List<string>();
    //Dictionary<string, List<string>> subqids = new Dictionary<string, List<string>>();
    foreach (var sub in subQiz)
    {
        var qids = sub.QizSubjectDefines.Select(v => v.QizItemID).ToList();
        //subqids.Add(sub.Title, qids);.a

        subQIDS.AddRange(qids);
    }
}           
@helper RenderCataAndQiTree(Catalog cata, List<string> qids, List<string> subQIDS)
    {
    <li class="closed">
        <div>
            <input type="checkbox" value='@(cata.CatalogID)' id='c@(cata.CatalogID)' onchange="$('#s@(cata.CatalogID) :checkbox').prop('checked',$(this).prop('checked'));"  />
            <label  for='c@(cata.CatalogID)'>@cata.Title</label>
        </div>
        @if (cata.QizItems.Count > 0 || cata.SubCatalogs.Count > 0)
        {
            var qis = cata.QizItems.Where(v => qids.Contains(v.QizItemID) || subQIDS.Contains(v.QizItemID));
            <ul id="s@(cata.CatalogID)">
                @if (cata.QizItems.Count > 0&&qis.Count()>0)
                {
                    <li class='qi'>
                        @foreach (var qi in qis)
                        {
                            <span style="border:1px solid gray; padding:1px;margin:1px;">
                                <input name="Selection" type="checkbox" value='@(qi.QizItemID)' id='q@(qi.QizItemID)'  />
                                <label  for='q@(qi.QizItemID)' title='@(qi.Title + ":" + qi.TypeRef.OptionTitle)'>@qi.Title</label>
                            </span>
                        }
                    </li>
                }
                @if (cata.SubCatalogs.Count > 0)
                {
                    foreach (var subcata in cata.SubCatalogs)
                    {
                        @RenderCataAndQiTree(subcata, qids, subQIDS);
                    }
                }
            </ul>
        }
    </li>
}
@using (Ajax.BeginForm(new AjaxOptions()
{
    LoadingElementId = "OnLoadingSpan",
    UpdateTargetId = ViewBag.utid,
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "Post"
    ,
    OnSuccess = "$('#dialogbox').dialog('close');",
    OnFailure = "OnSiteError"
}))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>基本信息</legend>
        @Html.HiddenFor(model => model.ID)
        @Html.HiddenFor(model => model.QizSmsSubjectID)
        本阶段截至月：
        @{var currmidxes = Model.QizSmsSubject.QizSmsSubjectMonthIdxes.OrderBy(v => v.MonthIdx).Select(v => v.MonthIdx).ToArray();}
        @Html.TextBoxFor(model => model.MonthIdx)
        <i>系统会自动连接到上一个阶段的截至月或出院日期。</i><br />
        <i>已经定义的阶段有：@(string.Join(",", currmidxes)) </i><br />
        <br />
        短信在出院后第
        @Html.EditorFor(model => model.SmsSendingMonthIdx)
        个月 第 @Html.EditorFor(model => model.SmsSendingDayIdx) 天发送。 <i>是相对出院开始的月数。</i>
    </fieldset>
    <fieldset>
        <legend>指标选择</legend>
        <div style="height: 300px; overflow: auto;">
            <ul id="monidxqitree">
            @RenderCataAndQiTree(ViewBag.RootCata as Catalog, QIDS, subQIDS)
            </ul>
        </div>
        <p>
            <input type="submit" value="确定" />
        </p>
    </fieldset>
}
<script type="text/javascript">
    $(function () {
        $('#monidxqitree li:not(:has(.qi))').attr('d', 'y')
        $('#monidxqitree li.qi').removeAttr('d')
        $('#monidxqitree li[d=y]').remove()
        $("#monidxqitree").treeview();
    });
</script>